package com.woniu.config;

import cn.hutool.core.lang.Snowflake;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
    //雪花id生成
    @Bean
    public Snowflake snowflake() {
        return new Snowflake();
    }

    //支付功能
    @Bean
    public AlipayClient alipayClient(AlipayConfig alipayConfig) {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                alipayConfig.getGatewayUrl(),
                alipayConfig.getApp_id(),
                alipayConfig.getMerchant_private_key(),
                "json",
                alipayConfig.getCharset(),
                alipayConfig.getAlipay_public_key(),
                alipayConfig.getSign_type()
        );
        return alipayClient;
    }

    //支付功能
    @Bean
    public AlipayTradePagePayRequest alipayTradePagePayRequest(AlipayConfig alipayConfig) {
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(alipayConfig.getReturn_url());
        alipayRequest.setNotifyUrl(alipayConfig.getNotify_url());

        return alipayRequest;
    }
}
